{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b040b690-6584-4ba0-8949-fdfb61c7912b",
   "metadata": {},
   "source": [
    "Chapter 08\n",
    "# 产生对角方阵\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3991468d-fd2d-49dc-ba99-5a50c64395cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 自定义函数产生对角方阵\n",
    "def diagonal_matrix(values):\n",
    "    size = len(values); matrix = []\n",
    "    for i in range(size):\n",
    "        row = [0] * size\n",
    "        matrix.append(row)\n",
    "        matrix[i][i] = values[i]\n",
    "    return matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8a7a3ce0-493e-41e7-b1c0-ddf8826867b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对角线元素\n",
    "diagonal_values = [4, 3, 2, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c9400b53-a986-48bb-b0b5-44a294b6d4a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[4, 0, 0, 0], [0, 3, 0, 0], [0, 0, 2, 0], [0, 0, 0, 1]]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用自定义函数\n",
    "diagonal_matrix = diagonal_matrix(diagonal_values)\n",
    "diagonal_matrix"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
